package com.luo.chengrui.module.sa.controller.admin.matrix;

import com.luo.chengrui.module.sa.controller.admin.org.vo.OrgRespVO;
import com.luo.chengrui.module.sa.convert.org.OrgConvert;
import com.luo.chengrui.module.sa.dal.dataobject.org.OrgDO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.matrix.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.matrix.OporgManagerMatrixConfigDO;
import com.luo.chengrui.module.sa.convert.matrix.OporgManagerMatrixConfigConvert;
import com.luo.chengrui.module.sa.service.matrix.OporgManagerMatrixConfigService;

@Tag(name = "管理后台 - 组织管理矩阵人员落位表 ")
@RestController
@RequestMapping("/sa/oporg-manager-matrix-config")
@Validated
public class OporgManagerMatrixConfigController {

    @Resource
    private OporgManagerMatrixConfigService oporgManagerMatrixConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建组织管理矩阵人员落位表 ")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:create')")
    public CommonResult<String> createOporgManagerMatrixConfig(@Valid @RequestBody OporgManagerMatrixConfigCreateReqVO createReqVO) {
        return success(oporgManagerMatrixConfigService.createOporgManagerMatrixConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织管理矩阵人员落位表 ")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:update')")
    public CommonResult<Boolean> updateOporgManagerMatrixConfig(@Valid @RequestBody OporgManagerMatrixConfigUpdateReqVO updateReqVO) {
        oporgManagerMatrixConfigService.updateOporgManagerMatrixConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织管理矩阵人员落位表 ")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:delete')")
    public CommonResult<Boolean> deleteOporgManagerMatrixConfig(@RequestParam("id") String id) {
        oporgManagerMatrixConfigService.deleteOporgManagerMatrixConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织管理矩阵人员落位表 ")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:query')")
    public CommonResult<OporgManagerMatrixConfigRespVO> getOporgManagerMatrixConfig(@RequestParam("id") String id) {
        OporgManagerMatrixConfigDO oporgManagerMatrixConfig = oporgManagerMatrixConfigService.getOporgManagerMatrixConfig(id);
        return success(OporgManagerMatrixConfigConvert.INSTANCE.convert(oporgManagerMatrixConfig));
    }

    @GetMapping("/list")
    @Operation(summary = "获得组织管理矩阵人员落位表 列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:query')")
    public CommonResult<List<OporgManagerMatrixConfigRespVO>> getOporgManagerMatrixConfigList(@RequestParam("ids") Collection<String> ids) {
        List<OporgManagerMatrixConfigDO> list = oporgManagerMatrixConfigService.getOporgManagerMatrixConfigList(ids);
        return success(OporgManagerMatrixConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织管理矩阵人员落位表 分页")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:query')")
    public CommonResult<PageResult<OporgManagerMatrixConfigRespVO>> getOporgManagerMatrixConfigPage(@Valid OporgManagerMatrixConfigPageReqVO pageVO) {
        PageResult<OporgManagerMatrixConfigDO> pageResult = oporgManagerMatrixConfigService.getOporgManagerMatrixConfigPage(pageVO);
        return success(OporgManagerMatrixConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织管理矩阵人员落位表  Excel")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:export')")
    @OperateLog(type = EXPORT)
    public void exportOporgManagerMatrixConfigExcel(@Valid OporgManagerMatrixConfigExportReqVO exportReqVO,
                                                    HttpServletResponse response) throws IOException {
        List<OporgManagerMatrixConfigDO> list = oporgManagerMatrixConfigService.getOporgManagerMatrixConfigList(exportReqVO);
        // 导出 Excel
        List<OporgManagerMatrixConfigExcelVO> datas = OporgManagerMatrixConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "组织管理矩阵人员落位表 .xls", "数据", OporgManagerMatrixConfigExcelVO.class, datas);
    }


    @GetMapping("/data")
    @Operation(summary = "查询矩阵配置信息。")
    @Parameter(name = "matrixId", description = "编号列表", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:query')")
    public CommonResult<OporgManagerMatrixConfigDataRespVO> getOporgManagerMatrixConfigData(@RequestParam("matrixId") String matrixId) {
        OporgManagerMatrixConfigDataRespVO respVO = oporgManagerMatrixConfigService.getOporgManagerMatrixConfigDataList(matrixId);
        return success(respVO);
    }

    @GetMapping("/use")
    @Operation(summary = "查询矩阵配置信息。")
//    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:query')")
    public CommonResult<OporgManagerMatrixConfigDataRespVO> getOporgManagerMatrixConfigData4Use() {
        OporgManagerMatrixConfigDataRespVO respVO = oporgManagerMatrixConfigService.getOporgManagerMatrixDataList4Use();
        return success(respVO);
    }

    @GetMapping("/matrix/person")
    @Operation(summary = "获取矩阵人员。")
    @Parameter(name = "matrixId", description = "编号列表", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix-config:query')")
    public CommonResult<OporgManagerMatrixPersonRespVO> getOrgManagerMatrixPerson(@Valid OporgManagerMatrixConfigReqVO vo) {
        OporgManagerMatrixPersonRespVO respVO = oporgManagerMatrixConfigService.getOrgManagerMatrixPerson(vo);
        return success(respVO);
    }

}
